package com.controller;

import java.io.InputStream;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.identity.Group;
import org.activiti.engine.impl.persistence.entity.GroupEntityImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 组织机构角色
 * @author 123
 *
 */
@RestController
public class CandidateGroupController {

	@Autowired
	private ProcessEngine processEngine;
	
	/**
	 * 部署
	 * @return
	 */
	@GetMapping("sequence/deployCandidateGroup")
	public String deploySequenceFlow(){
		
		InputStream bpmn = this.getClass().getResourceAsStream("/diagrams/cadinerGroup.bpmn");
		InputStream png = this.getClass().getResourceAsStream("/diagrams/cadinerGroup.png");
		
		Deployment deployment = processEngine.getRepositoryService().createDeployment().name("组任务")
			.addInputStream("cadinerGroup.bpmn", bpmn)//与资源文件名称要一致
			.addInputStream("cadinerGroup.png", png)
			.deploy();
		
		System.err.println(deployment.getKey() + "---" + deployment.getName());
		
		//添加用户角色组
		IdentityService identityService = processEngine.getIdentityService();
		
		//创建角色
		//identityService.saveGroup(Group);;
		
		//创建用户
		//identityService.saveUser(user);
		
		//创建角色用户的关联关系
		//identityService.createMembership(userId, groupId);
		
		System.out.println("添加组织机构成功！");
		
		return deployment.getId();
	}
	
	/**
	 * 启动流程实例
	 * @return
	 */
	@RequestMapping("sequence/startCandidateGroup")
	public String startCandidateGroupFlow(){
		
		String processDefinitionKey = "candidateGroup";
		
		//正在执行的流程实例和执行对象相关的service
		RuntimeService runtimeService = processEngine.getRuntimeService();
		
		//使用流程定义的key启动流程实例，key对应的是bpmn文件中的id，默认是最新版本的流程启动
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);
		
		System.out.println(processInstance.getId() + processInstance.getProcessDefinitionId());
		
		return processInstance.getProcessDefinitionId();
	}
	
}
